package com.quark.controller.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quark.annotation.SysLog;
import com.quark.constant.CacheConstants;
import com.quark.model.system.dict.DictVO;
import com.quark.model.system.dict.SysDict;
import com.quark.model.system.dict.SysDictItem;
import com.quark.result.RequestResult;
import com.quark.service.system.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 张高昌
 * @date 2022/01/09 13:59
 * @description 字典表
 */
@Api(tags = "字典管理")
@RestController
@RequestMapping("/dict")
@RequiredArgsConstructor
public class DictController {

    private final DictService dictService;

    /**
     * 字典表分页查询
     *
     * @param page
     * @param sysDict
     * @return
     * @author 张高昌
     * @date 2022/01/09 15:12
     */
    @ApiOperation("字典列表分页查询")
    @GetMapping("/dictPageList")
    public RequestResult<IPage<SysDict>> dictPageList(Page page, SysDict sysDict) {

        return RequestResult.success(dictService.getDictPage(page, sysDict));
    }

    /**
     * 新增字典表
     *
     * @param vo
     * @return
     * @author 张高昌
     * @date 2022/02/07 17:16
     */
    @SysLog("添加字典")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_dict_add')")
    public RequestResult saveDict(@RequestBody @Valid DictVO vo) {
        SysDict sysDict = new SysDict();
        BeanUtil.copyProperties(vo, sysDict, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return RequestResult.success(dictService.saveDict(sysDict));
    }

    /**
     * 修改字典表
     *
     * @param sysDict
     * @return
     * @author 张高昌
     * @date 2022/02/07 17:19
     */
    @SysLog("修改字典")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_dict_edit')")
    public RequestResult updateDict(@RequestBody @Valid SysDict sysDict) {
        return dictService.updateDict(sysDict);
    }

    /**
     * 删除字典
     *
     * @param id
     * @return
     * @author 张高昌
     * @date 2022/02/07 16:22
     */
    @SysLog("删除字典")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_dict_del')")
    public RequestResult removeById(@PathVariable Integer id) {
        return dictService.removeDict(id);
    }

    /**
     * 字典项分页查询
     *
     * @param page
     * @param sysDictItem
     * @return
     * @author 张高昌
     * @date 2022/01/09 19:42
     */
    @ApiOperation("字典项分页查询")
    @GetMapping("/item/PageList")
    public RequestResult<IPage<SysDictItem>> dictItemPageList(Page page, SysDictItem sysDictItem) {

        return RequestResult.success(dictService.getDictItemPage(page, sysDictItem));
    }

    /**
     * 新增字典项并且同步缓存
     *
     * @param sysDictItem
     * @return
     * @author 张高昌
     * @date 2022/02/07 09:27
     */
    @SysLog("新增字典项")
    @PostMapping("/item")
    @CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
    public RequestResult saveDictItem(@RequestBody @Valid SysDictItem sysDictItem) {

        return dictService.addDictItem(sysDictItem);
    }

    /**
     * 修改字典项
     *
     * @param sysDictItem
     * @return
     * @author 张高昌
     * @date 2022/02/07 09:27
     */
    @SysLog("修改字典项")
    @PutMapping("/item")
    public RequestResult updateById(@RequestBody @Valid SysDictItem sysDictItem) {

        return dictService.updateDictItem(sysDictItem);
    }

    /**
     * 根据id删除字典项
     *
     * @param id
     * @return
     * @author 张高昌
     * @date 2022/02/07 09:27
     */
    @SysLog("删除字典项")
    @DeleteMapping("/item/{id}")
    public RequestResult removeDictItemById(@PathVariable Integer id) {

        return RequestResult.success(dictService.deleteDictItem(id));
    }
}
